package com.lvtulife.common.util.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Configuration {
    private Logger logger = LoggerFactory.getLogger(Configuration.class);
    private Properties propertie;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;


    public Configuration() {
        propertie = new Properties();
    }


    public Configuration(String filePath) {
        propertie = new Properties();
        try {
            //inputFile = new FileInputStream(filePath);
            propertie.load(this.getClass().getClassLoader().getResourceAsStream(filePath));
            //  propertie.load(inputFile);
            //inputFile.close();
        } catch (FileNotFoundException ex) {
            logger.error("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在", ex);
        } catch (IOException ex) {
            logger.error("装载文件--->失败!", ex);
        }
    }//end ReadConfigInfo()


    public String getValue(String key) {
        if (propertie.containsKey(key)) {
            String value = propertie.getProperty(key);//得到某一属性的值
            return value;
        } else
            return "";
    }//end getValue()


    public String getValue(String fileName, String key) {
        try {
            String value = "";
            inputFile = new FileInputStream(fileName);
            propertie.load(inputFile);
            inputFile.close();
            if (propertie.containsKey(key)) {
                value = propertie.getProperty(key);
                return value;
            } else
                return value;
        } catch (FileNotFoundException e) {
            logger.error("获取配置文件属性异常：", e);
        } catch (IOException e) {
            logger.error("获取配置文件属性异常：", e);
        } catch (Exception e) {
            logger.error("获取配置文件属性异常：", e);
        }
        return "";
    }//end getValue()


    public void clear() {
        propertie.clear();
    }//end clear();


    public void setValue(String key, String value) {
        propertie.setProperty(key, value);
    }//end setValue()


    public void saveFile(String fileName, String description) {
        try {
            outputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) {
            logger.error("保存配置文件属性异常：", e);
        } catch (IOException e) {
            logger.error("保存配置文件属性异常：", e);
        }
    }//end saveFile()

    public static void main(String[] args) {
        Configuration rc = new Configuration("messages.properties");//相对路径

        String ip = rc.getValue("logout.delete");//以下读取properties文件的值
        String host = rc.getValue("datasource.url");
        String tab = rc.getValue("datasource.username");
        String password = rc.getValue("datasource.password");

       /* logger.info("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下输出properties读出的值
        logger.info("ip's length = " + ip.length());
        logger.info("host = " + host);
        logger.info("tab = " + tab);*/
//        cf.clear();


//        Configuration saveCf = new Configuration();
//        saveCf.setValue("min", "10");
//        saveCf.setValue("max", "1000");
//        saveCf.saveFile(".\config\save.perperties");

    }//end main()

}